package cn.maxwell.controller;

import cn.maxwell.FeiClient.ProductFeiGinClient;
import cn.maxwell.domain.Store;
import cn.maxwell.server.IStoreServers;
import cn.maxwell.util.JsonResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/store")
public class StoreController {

    @Resource
    private ProductFeiGinClient productFeiGinClient;

    @Resource
    private IStoreServers storeServers;

    @Value("${server.port}")
    private Integer port;

    @GetMapping("/loadById/{id}")
    public Store loadById(@PathVariable("id") Long id,HttpServletRequest request) throws Exception {
        Store store = storeServers.loadById(id,request);
        return store;
    }

    @PostMapping("/add")
    public JsonResult add(@RequestBody Store store){
        try {
            storeServers.add(store);
            return new JsonResult(true,"操作成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"操作失败！");
        }
    }

    //浏览器来掉
    @RequestMapping("/store/{id}")
    public JsonResult getById(@PathVariable("id")Long id){
        try {
            //使用Feign调用用户服务获取product
            productFeiGinClient.loadById(id);
            return new JsonResult(true,"操作成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"操作失败！");
        }
    }
}
